import 'webextension-polyfill';
import { exampleThemeStorage } from '@chrome-extension-boilerplate/storage';

exampleThemeStorage.get().then(theme => {
  console.log('theme', theme);
});

console.log('background loaded');
console.log("Edit 'apps/chrome-extension/lib/background/index.ts' and save to reload.");

chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: 'openSidePanel',
    title: 'Open side panel',
    contexts: ['all']
  });
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
  if (info.menuItemId === 'openSidePanel' && tab) {
    // This will open the panel in all the pages on the current window.
    chrome.sidePanel.open({ windowId: tab.windowId });
  }
});
let sidePanelVisible = false;
chrome.runtime.onMessage.addListener((message, sender) => {
  if (message.action === 'openSidePanel' && sender.tab) {
    if (sidePanelVisible) {
      chrome.sidePanel.setOptions({ enabled: false });
    } else {
      chrome.sidePanel.setOptions({ enabled: true });
      chrome.sidePanel.open({ windowId: sender.tab.windowId });
    }
    sidePanelVisible = !sidePanelVisible;
  }
  return true;
});